﻿<%@ WebHandler Language="C#" Class="CreateImage" %>

using System;
using System.Web;
using System.Web.SessionState;
public class CreateImage : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {
        //object obj = context.Session["url"];
        
        
        
        string szLat = context.Request.QueryString["lat"];
        string szLon = context.Request.QueryString["lon"];
        double dLat = Double.Parse(szLat);
        double dLon = Double.Parse(szLon);
        int nLevel =  Int32.Parse(context.Request.QueryString["level"]);
        int nH = Convert.ToInt32(context.Request.QueryString["h"]);
        int nW = Convert.ToInt32(context.Request.QueryString["w"]);
        int nSize = nW < nH ? nW : nH;
        byte[] b = CreateImageMymap(dLat, dLon, nLevel, nSize);
        context.Response.ContentType = "image/png";
        context.Response.OutputStream.Write(b, 0, b.Length);
       // context.Response.Flush();
       // context.Response.Clear();
        //context.Response.Close();
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    protected byte[] CreateImageMymap(double dLat, double dLon, int nLevel, int siZe)
    {

        System.Drawing.Bitmap curBitmap = new System.Drawing.Bitmap(siZe, siZe);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(curBitmap);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        System.Drawing.Rectangle clientRect = new System.Drawing.Rectangle(0, 0, siZe, siZe);

        Transform transform = new Transform();
        transform.Setup(clientRect, nLevel, dLon, dLat);

        ImageCellMngr imageCellMngr = new ImageCellMngr();
        imageCellMngr.LoadMapImage(transform);
        imageCellMngr.Draw(g, transform);

        Point2D vpt = transform.W2V(dLon, dLat);
        //Rectangle vptRect = new Rectangle((int)vpt.x - 5, (int)vpt.y - 5, 10, 10);

        // Draw and fill rectangles
        //SolidBrush fillBrush = new SolidBrush(Color.Red);
        //g.FillEllipse(fillBrush, vptRect);

        System.Drawing.Bitmap posImg = new System.Drawing.Bitmap(HttpContext.Current.Request.PhysicalApplicationPath + "Images\\position.png");
        g.DrawImage(posImg, (float)(vpt.x - 8), (float)(vpt.y - 16));

        // Save the Bitmap object and send response to the
        // browser
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        curBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        byte[] data = ms.ToArray();
        
        // Dispose of Graphics and Bitmap objects
        curBitmap.Dispose();
        g.Dispose();
        ms.Close();
        return data;
    }

}